package com.pb.metadata.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pb.infra.oth.util.Constant;
import com.pb.infra.oth.util.JwtTokenConfig;
import com.pb.infra.oth.util.StringUtil;
import com.pb.metadata.mapper.DbUserManagementMapper;
import com.pb.infra.oth.entity.common.Page;
import com.pb.infra.oth.entity.common.Result;
import io.jsonwebtoken.Claims;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 数据库用户管理
 * @author liaohaoyuan
 * @date 2021/2/25
 */
@Service
public class DbUserManagementService {

    /**
     * 数据库用户DAO映射对象
     */
    @Resource
    private DbUserManagementMapper dbUserManagementMapper;

    /**
     * jwttoken配置类
     */
    @Resource
    private JwtTokenConfig jwtTokenConfig;

    /**
     * 查询数据库用户分页数据
     * @param map 查询参数
     * @param page 页码
     * @return
     */
    public Result select(Map<String, Object> map, Page page){
        if (page != null && page.getPageIndex() != null && page.getPageSize() != null) {
            PageHelper.startPage(page.getPageIndex(), page.getPageSize());
        }
        String dbUserName=(String) map.get("dbUserName");
        if (!"".equals(dbUserName) && dbUserName != null){
            map.put("dbUserName",dbUserName.toUpperCase(Locale.ENGLISH).trim());
        }
        List<Map<String, Object>> list = dbUserManagementMapper.select(map);
        return Result.ok(new PageInfo(list).getTotal(), list);
    }

    /**
     * 数据库用户管理新建数据
     * @param paramMap 保存数据
     * @return
     */
    public Result insert(Map<String, Object> paramMap, HttpServletRequest request){
        paramMap.put("dbUserId", UUID.randomUUID().toString().replace("-", ""));
        String str = (String)paramMap.get("dbUser");
        paramMap.put("dbUser",str.toUpperCase(Locale.ENGLISH));
        String token = request.getHeader(jwtTokenConfig.getHeader());
        Claims claims = jwtTokenConfig.getClaimByToken(token);
        String userId = (String)claims.get(jwtTokenConfig.getSub());
        paramMap.put("createUser",userId);
        String checkoutData = dbUserManagementMapper.getCheckoutData(paramMap);
        if (checkoutData!=null && !"".equals(checkoutData)){
            paramMap.put("dbUserId",checkoutData);
            paramMap.put("updateUser",(String)paramMap.get("createUser"));
            this.update(paramMap, request);
        }else{
            dbUserManagementMapper.insert(paramMap);
        }
        return Result.OK_EMPTY;
     }

    /**
     * 数据库用户管理修改
     * @return
     */
     public  Result update(Map<String, Object> paramMap, HttpServletRequest request){
         String token = request.getHeader(jwtTokenConfig.getHeader());
         Claims claims = jwtTokenConfig.getClaimByToken(token);
         String userId = (String)claims.get(jwtTokenConfig.getSub());
         paramMap.put("updateUser", userId);
         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
         paramMap.put("updateTime",format.format(new Date()));
         String str = (String)paramMap.get("dbUser");
         paramMap.put("dbUser",str.toUpperCase(Locale.ENGLISH));
         if (!StringUtil.isEmpty(paramMap.get("checkFlag")) && "1".equals(paramMap.get("checkFlag").toString())) {
             String checkoutData = dbUserManagementMapper.getCheckoutData(paramMap);
             if (!StringUtil.isEmpty(checkoutData)) {
                 return Result.error(Constant.EXIST,"该数据库用户已存在");
             }
         }
         dbUserManagementMapper.update(paramMap);
         return Result.OK_EMPTY;
     }

    /**
     * 获取数据库数据
     * @return
     */
     public List<Map<String,Object>> getDatabaseList(){
         List<Map<String,Object>> list = dbUserManagementMapper.getDatabaseList();
         return list;
     }

    /**
     * 删除数据库用户
     * @param list
     */
     public void delete(List<String> list){
        this.dbUserManagementMapper.delete(list);
    }

    /**
     * 启用停用数据库用户
     * @param paramMap
     */
    public void startOrStop( List<Map<String,Object>> paramMap){
        for (Map<String,Object> map:paramMap){
            String str = (String) map.get("activeFlag");
            map.put("activeFlag","1".equals(str)?"0":"1");
            dbUserManagementMapper.startOrStop(map);
        }
    }
}
